package email

import (
	"autoDeploy/config"
	"autoDeploy/dao/myselfMysql"
	"autoDeploy/models"
)

var db, _ = myselfMysql.CreateMySQLClient(config.MysqlDb)

//	GetAllEmails
//
// 描述信息： 根据用户id查询该用户的全部邮件
//
// Param：
//
//	userID ：用户id
//
// Return：
//
//	[]models.Notification ：用户的全部电子邮件
//	error ：错误信息
func GetAllEmails(userID uint64) ([]NotificationResponse, error) {

	emails, err := models.Notification{}.QueryNotificationsByReceiverID(db, userID)
	if err != nil {
		return nil, err
	}
	// 格式化日期并转换为NotificationResponse切片
	var formattedEmails []NotificationResponse
	for _, email := range emails {
		formattedEmails = append(formattedEmails, NotificationResponse{
			ID:               email.ID,
			SenderID:         email.SenderID,
			SenderName:       email.SenderName,
			ReceiverID:       email.ReceiverID,
			ReceiverName:     email.ReceiverName,
			TeamID:           email.TeamID,
			TeamName:         email.TeamName,
			NotificationType: email.NotificationType,
			Message:          email.Message,
			IsRead:           email.IsRead,
			CreatedAt:        email.CreatedAt.Format("2006-01-02"),
			UpdatedAt:        email.UpdatedAt.Format("2006-01-02"),
		})
	}

	return formattedEmails, nil

}
